//
//  SearchViewController.m
//  项目A --半糖
//
//  Created by romana on 16/6/1.
//  Copyright © 2016年 romana. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchCollectionViewCell.h"
#import "SearchDetailsViewController.h"



#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface SearchViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *myCollectionView;
@property (nonatomic, strong) NSArray *textArr;
@property (nonatomic, strong) UISearchBar *search;


@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
  //  self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:218 / 255.0 green:82 / 255.0 blue:146 / 255.0 alpha:1];
    
    // 处理导航栏
    [self handelNavigation];
    // 下边的大家都在搜
    [self createCollectionView];
    self.textArr = @[@"抹茶",@"双肩包",@"手机壳",@"手表",@"杯子",@"钱包",@"情侣",@"手链",@"项链",@"雨伞",@"戒指",@"耳钉"];
    
    
}


-(void)createCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.itemSize = CGSizeMake((WIDTH - 5)/4, WIDTH / 12);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64 + 20, WIDTH, HEIGHT / 4) collectionViewLayout:layout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    collectionView.backgroundColor = [UIColor whiteColor];
    self.myCollectionView = collectionView;
    [collectionView registerClass:[SearchCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.textArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    cell.nameLable.text = self.textArr[indexPath.row];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@",self.textArr[indexPath.row]);
    SearchDetailsViewController *searchVC = [[SearchDetailsViewController alloc] init];
    searchVC.searchName = self.textArr[indexPath.row];
    [self.navigationController pushViewController:searchVC animated:YES];
    
}


// 导航
-(void)handelNavigation{
    // 中间
    self.search = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 50, 38)];
    self.navigationItem.titleView = self.search;
    self.search.placeholder = @"搜索礼物~ 攻略";
    
    // 右侧
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:(UIBarButtonItemStylePlain) target:self action:@selector(searchRight:)];
}
-(void)searchRight:(UIBarButtonItem *)right{
    SearchDetailsViewController *searchVC = [[SearchDetailsViewController alloc] init];
    searchVC.searchName = self.search.text;
    [self.navigationController pushViewController:searchVC animated:YES];
    
}









@end
